package com.lee.dutylower.task;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.SystemClock;
import android.widget.ProgressBar;

import com.lee.dutylower.MyApplication;
import com.lee.dutylower.db.greendao.PostRecordDao;
import com.lee.dutylower.utils.ExcelUtil;

/**
 * Created by lijie on 2017/7/20.
 */

public class CreateExcelTask extends AsyncTask<Void,String,Void> {
    private ProgressDialog mProgress;
    private Context context;
    private int week;
    private  String[] weeks = new String[]{"周一","周二","周三","周四","周五","周六","周日"};
    private PostRecordDao postRecordDao;
    public CreateExcelTask(Context context, int week) {
        this.context = context;
        this.week = week;
        postRecordDao = MyApplication.getApplication().getDaoSession().getPostRecordDao();
    }

    @Override
    protected Void doInBackground(Void... voids) {
        SystemClock.sleep(1000);
        try {
            ExcelUtil.UpdateExcelByTemplate(context,postRecordDao,week);
        } catch (Exception e) {
            e.printStackTrace();
        }
        publishProgress("文件生成成功，路径："+ Environment.getExternalStorageDirectory().getAbsolutePath()+"/PostExcel/duty_lower_week"+week+".xls");
        SystemClock.sleep(3000);
        publishProgress("finish");
        return null;
    }

    @Override
    protected void onPreExecute() {
        mProgress = new ProgressDialog(context);
        mProgress.setTitle("Excel 生成");
        mProgress.setMessage("正在生成 "+weeks[week-1]+" 排班表 - - - - ");
        mProgress.setCanceledOnTouchOutside(false);
        mProgress.show();
    }



    @Override
    protected void onProgressUpdate(String... values) {
        if (values[0].equals("finish")){
            mProgress.cancel();
        }
        mProgress.setMessage(values[0]);

    }
}
